const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')

const app = new Koa()

// 解析请求体
app.use(bodyParser())

// 创建路由实例
const router = new Router()

// 引入数据库管理模块
const dbManager = require('../db/dbmanager')

// 接收反馈信息并存储到数据库中
router.post('/public/content', async (ctx, next) => {

  try {
    const { openid, opinion, content } = ctx.request.body
 
    // 插入反馈信息到数据库中
    const sql = 'INSERT INTO feedback (openid, solution, contact) VALUES (?, ?, ?)'
    const result = await dbManager.query(sql, [openid, opinion, content])

    // 返回响应结果
    ctx.type = 'application/json'
    ctx.body = { code: 0, message: 'success', result }
  } catch (err) {
    console.error('提交反馈失败:', err)
    ctx.type = 'application/json'
    ctx.body = { code: -1, message: err.message }
  }
})
module.exports = router
